'use strict'
import Vue from 'vue'
import Router from 'vue-router'
// import Cookies from 'js-cookie'
// import request from '@/utils/request'
import { loadScript } from '@/utils'
import { isURL } from '@/utils/validate'

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch((err) => err)
}

Vue.use(Router)

// 页面路由(独立页面)
export const pageRoutes = [
  {
    path: '/zfSafetyProduction1Link',
    component: () =>
      import('@/views/modules/zfLink/zfSafetyProduction1'),
    name: 'zfSafetyProduction1Link',
    meta: {
      title: '生产安全',
      isTab: true
    }
  },
  {
    path: '/zfSafetyProduction2Link',
    component: () =>
      import('@/views/modules/zfLink/zfSafetyProduction2'),
    name: 'zfSafetyProduction2Link',
    meta: {
      title: '生产安全',
      isTab: true
    }
  },
  {
    path: '/zfEnergyConsumptionLink',
    component: () =>
      import('@/views/modules/zfLink/zfEnergyConsumption1'),
    name: 'zfEnergyConsumptionLink',
    meta: {
      title: '能源消耗',
      isTab: true
    }
  },
  {
    path: '/zfEnvironmentalProtectionLink',
    component: () =>
      import('@/views/modules/zfLink/zfEnvironmentalProtection1'),
    name: 'zfEnvironmentalProtectionLink',
    meta: {
      title: '环境保护',
      isTab: true
    }
  },
  {
    path: '/zfParkManagementLink',
    component: () =>
      import('@/views/modules/zfLink/zfParkManagement1'),
    name: 'zfParkManagementLink',
    meta: {
      title: '园区管理',
      isTab: true
    }
  },
  {
    path: '/zfParkDetailLink',
    component: () =>
      import('@/views/modules/zfLink/zfParkDetail'),
    name: 'zfParkDetailLink',
    meta: {
      title: '园区详情',
      isTab: true
    }
  },
  {
    path: '/zfParkAerialViewLink',
    component: () =>
      import('@/views/modules/zfLink/zfParkAerialView'),
    name: 'zfParkAerialViewLink',
    meta: {
      title: '园区鸟瞰',
      isTab: true
    }
  },
  {
    path: '/zfIntelligentDiagnosticLink',
    component: () =>
      import('@/views/modules/zfLink/zfIntelligentDiagnostic'),
    name: 'zfIntelligentDiagnosticLink',
    meta: {
      title: '智能制造诊断评估',
      isTab: true
    }
  },
  {
    path: '/zfIndustryOverview',
    component: () =>
      import('@/views/modules/zf/zfIndustryOverview'),
    name: 'zfIndustryOverview',
    meta: {
      title: '产业总览',
      isTab: true
    }
  },
  // {
  //   path: '/zfIntelligentDiagnostic',
  //   component: () =>
  //     import('@/views/modules/zf/zfIntelligentDiagnostic'),
  //   name: 'zfIntelligentDiagnostic',
  //   meta: {
  //     title: '智能制造诊断',
  //     isTab: true
  //   }
  // },
  {
    path: '/zfDiagnosticEvaluation',
    component: () =>
      import('@/views/modules/zf/zfIntelligentDiagnostic'),
    name: 'zfDiagnosticEvaluation',
    meta: {
      title: '智能制造诊断评估',
      isTab: true
    }
  },
  {
    path: '/zfPerMuBenefit',
    component: () =>
      import('@/views/modules/zf/zfPerMuBenefit'),
    name: 'zfPerMuBenefit',
    meta: {
      title: '亩均效益',
      isTab: true
    }
  },

  {
    path: '/zfEnvironmentalConservation',
    component: () =>
      import('@/views/modules/zf/zfEnvironmentalConservation'),
    name: 'zfEnvironmentalConservation',
    meta: {
      title: '环境保护',
      isTab: true
    }
  },
  {
    path: '/zfSafetyOverview',
    component: () =>
      import('@/views/modules/zf/zfSafetyOverview'),
    name: 'zfSafetyOverview',
    meta: {
      title: '安全生产',
      isTab: true
    }
  },
  {
    path: '/zfContingencyManagement',
    component: () =>
      import('@/views/modules/zf/zfContingencyManagement'),
    name: 'zfContingencyManagement',
    meta: {
      title: '应急管理',
      isTab: true
    }
  },
  {
    path: '/zfParkOverview',
    component: () =>
      import('@/views/modules/zf/zfParkOverview'),
    name: 'zfParkOverview',
    meta: {
      title: '园区总览',
      isTab: true
    }
  }, {
    path: '/zfParkOverviewDetail',
    component: () =>
      import('@/views/modules/zf/zfParkOverviewDetail'),
    name: 'zfParkOverviewDetail',
    meta: {
      title: '园区总览详情',
      isTab: true
    }
  },
  {
    path: '/zfDangerousGoods',
    component: () =>
      import('@/views/modules/zf/zfDangerousGoods'),
    name: 'zfDangerousGoods',
    meta: {
      title: '危化品总览',
      isTab: true
    }
  },
  {
    path: '/zfDataSharing',
    component: () =>
      import('@/views/modules/zf/zfDataSharing'),
    name: 'zfDataSharing',
    meta: {
      title: '数据共享',
      isTab: true
    }
  },
  {
    path: '/zfIndustrialChainMap',
    component: () =>
      import('@/views/modules/zf/zfIndustrialChainMap'),
    name: 'zfIndustrialChainMap',
    meta: {
      title: '产业链图谱',
      isTab: true
    }
  },
  {
    path: '/zf',
    component: () => import('@/views/modules/zf/zf'),
    name: 'zf',
    meta: {
      title: '生产安全',
      noNeedToken: true
    },
    redirect: {
      name: 'zfSafetyProduction'
    },
    children: [
      // {
      //   path: '/zfSafetyProduction',
      //   component: () =>
      //     import('@/views/modules/zf/zfSafetyProduction'),
      //   name: 'zfSafetyProduction',
      //   meta: {
      //     title: '生产安全',
      //     isTab: true
      //   }
      // },
      {
        path: '/zfSafetyProduction',
        component: () =>
          import('@/views/modules/zf/zfSafetyProduction1'),
        name: 'zfSafetyProduction',
        meta: {
          title: '生产安全',
          isTab: true
        }
      },
      {
        path: '/zfSafetyProduction2',
        component: () =>
          import('@/views/modules/zf/zfSafetyProduction2'),
        name: 'zfSafetyProduction2',
        meta: {
          title: '生产安全',
          isTab: true
        }
      },
      // {
      //   path: '/zfEnergyConsumption',
      //   component: () =>
      //     import('@/views/modules/zf/zfEnergyConsumption'),
      //   name: 'zfEnergyConsumption',
      //   meta: {
      //     title: '能源消耗',
      //     isTab: true
      //   }
      // },
      {
        path: '/zfEnergyConsumption',
        component: () =>
          import('@/views/modules/zf/zfEnergyConsumption1'),
        name: 'zfEnergyConsumption',
        meta: {
          title: '能源消耗',
          isTab: true
        }
      },
      // {
      //   path: '/zfEnvironmentalProtection',
      //   component: () =>
      //     import('@/views/modules/zf/zfEnvironmentalProtection'),
      //   name: 'zfEnvironmentalProtection',
      //   meta: {
      //     title: '环境保护',
      //     isTab: true
      //   }
      // },
      {
        path: '/zfEnvironmentalProtection',
        component: () =>
          import('@/views/modules/zf/zfEnvironmentalProtection1'),
        name: 'zfEnvironmentalProtection',
        meta: {
          title: '环境保护',
          isTab: true
        }
      },
      // {
      //   path: '/zfParkManagement',
      //   component: () =>
      //     import('@/views/modules/zf/zfParkManagement'),
      //   name: 'zfParkManagement',
      //   meta: {
      //     title: '园区管理',
      //     isTab: true
      //   }
      // },
      {
        path: '/zfParkManagement',
        component: () =>
          import('@/views/modules/zf/zfParkManagement1'),
        name: 'zfParkManagement',
        meta: {
          title: '园区管理',
          isTab: true
        }
      },
      {
        path: '/zfParkAerialView',
        component: () =>
          import('@/views/modules/zf/zfParkAerialView'),
        name: 'zfParkAerialView',
        meta: {
          title: '园区鸟瞰',
          isTab: true
        }
      },
      {
        path: '/zfSafetyProductionDetail',
        component: () =>
          import('@/views/modules/zf/zfSafetyProductionDetail'),
        name: 'zfSafetyProductionDetail',
        meta: {
          title: '生产安全园区详情',
          isTab: true
        }
      },
      {
        path: '/zfEnergyConsumptionDetail',
        component: () =>
          import('@/views/modules/zf/zfEnergyConsumptionDetail'),
        name: 'zfEnergyConsumptionDetail',
        meta: {
          title: '能源消耗园区详情',
          isTab: true
        }
      },
      {
        path: '/zfEnvironmentalProtectionDetail',
        component: () =>
          import('@/views/modules/zf/zfEnvironmentalProtectionDetail'),
        name: 'zfEnvironmentalProtectionDetail',
        meta: {
          title: '环境保护园区详情',
          isTab: true
        }
      },
      {
        path: '/zfParkManagementDetail',
        component: () =>
          import('@/views/modules/zf/zfParkManagementDetail'),
        name: 'zfParkManagementDetail',
        meta: {
          title: '园区管理园区详情',
          isTab: true
        }
      },
      {
        path: '/zfParkDetail',
        component: () =>
          import('@/views/modules/zf/zfParkDetail'),
        name: 'zfParkDetail',
        meta: {
          title: '园区详情',
          isTab: true
        }
      },
      {
        path: '/tanpaifang',
        component: () =>
          import('@/views/modules/zf/tanpaifang'),
        name: 'tanpaifang',
        meta: {
          title: '碳排放',
          isTab: true
        }
      },
      {
        path: '/commandCentre',
        component: () =>
          import('@/views/modules/zf/commandCentre'),
        name: 'commandCentre',
        meta: {
          title: '地图聚合',
          isTab: true
        }
      }
    ]
  },
  {
    path: '/jump',
    component: () => import('@/views/modules/defaultIndex/index'),
    name: 'jump',
    meta: {
      title: '首页'
    }
  },
  {
    path: '/home',
    component: () => import('@/views/modules/zf/The overview/home'),
    name: 'home',
    meta: {
      title: 'home'
    }
  },
  {
    path: '/ChemicalIndustry',
    component: () => import('@/views/modules/zf/The overview/ChemicalIndustry'),
    name: 'ChemicalIndustry',
    meta: {
      title: '产业总览2.0',
      num: 0
    }
  },
  {
    path: '/zfPerMuBenefitTwo',
    component: () =>
      import('@/views/modules/zf/The overview/zfPerMuBenefitTwo'),
    name: 'zfPerMuBenefitTwo',
    meta: {
      title: '亩均效益2.0',
      isTab: true,
      num: 1

    }
  },
  //  {
  //   path: '/zfParkOverviewTwo',
  //   component: () => import('@/views/modules/zf/The overview/zfParkOverviewTwo'),
  //   name: 'zfParkOverviewTwo',
  //   meta: {
  //     title: '园区总览2.0',
  //     num: 2
  //   }
  // },
  {
    path: '/zfParkOverviewTwo',
    component: () => import('@/views/modules/zf/The overview/zfParkOverviewTwo2'),
    name: 'zfParkOverviewTwo',
    meta: {
      title: '园区管理2.0',
      num: 2
    }
  },
  {
    path: '/zfParkOverviewDetailTwo',
    component: () => import('@/views/modules/zf/The overview/zfParkOverviewDetailTwo'),
    name: 'zfParkOverviewDetailTwo',
    meta: {
      title: '园区总览详情2.0'
    }
  },
  {
    path: '/zfParkOverviewDetailtwin',
    component: () => import('@/views/modules/zf/The overview/zfParkOverviewDetailtwin'),
    name: 'zfParkOverviewDetailtwin',
    meta: {
      title: '园区总览详情3.0'
    }
  },
  {
    path: '/zfDiagnosticEvaluationTwo',
    component: () => import('@/views/modules/zf/The overview/zfIntelligentDiagnosticTwo'),
    name: 'zfDiagnosticEvaluationTwo',
    meta: {
      title: '智能制造诊断2.0',
      num: 3
    }
  },
  {
    path: '/zfSafetyOverviewTwo',
    component: () => import('@/views/modules/zf/The overview/zfSafetyOverviewTwo'),
    name: 'zfSafetyOverviewTwo',
    meta: {
      title: '安全生产2.0',
      num: 4
    }
  },
  {
    path: '/zfSafeAndContingency',
    component: () =>
      import('@/views/modules/zf/The overview/zfSafeAndContingency'),
    name: 'zfSafeAndContingency',
    meta: {
      title: '安全生产2.0',
      num: 4
    }
  },
  {
    path: '/zfoperationalmonitoring',
    component: () => import('@/views/modules/zf/The overview/zfoperationalmonitoring'),
    name: 'zfoperationalmonitoring',
    meta: {
      title: '产业运行监测2.0',
      num: 5
    }
  },
  {
    path: '/zfContingencyManagementTwo',
    component: () => import('@/views/modules/zf/The overview/zfContingencyManagementTwo'),
    name: 'zfContingencyManagementTwo',
    meta: {
      title: '应急总览2.0',
      num: 5
    }
  },
  {
    path: '/zfDangerousGoodsTwo',
    component: () => import('@/views/modules/zf/The overview/zfDangerousGoodsTwo'),
    name: 'zfDangerousGoodsTwo',
    meta: {
      title: '危化品总览2.0',
      num: 6
    }
  },
  {
    path: '/zfEnvironmentalConservationTwo',
    component: () => import('@/views/modules/zf/The overview/zfEnvironmentalConservationTwo'),
    name: 'zfEnvironmentalConservationTwo',
    meta: {
      title: '环境保护2.0',
      num: 7
    }
  },
  {
    path: '/zfDataSharingTwo',
    component: () => import('@/views/modules/zf/The overview/zfDataSharingTwo'),
    name: 'zfDataSharingTwo',
    meta: {
      title: '数据共享2.0',
      num: 8
    }
  },
  {
    path: '/zfIndustrialChainMapTwo',
    component: () => import('@/views/modules/zf/The overview/zfIndustrialChainMapTwo'),
    name: 'zfIndustrialChainMapTwo',
    meta: {
      title: '产业链图谱2.0',
      num: 9
    }
  },
  {
    path: '/MakingDiagnosis',
    component: () => import('@/views/modules/zf/The overview/MakingDiagnosis'),
    name: 'MakingDiagnosis',
    meta: {
      title: '产业链图谱2.0'

    }
  }
]

// isTab: true
// 模块路由(基于主入口布局页面)
export const moduleRoutes = {
  path: '/',
  // beforeEnter () {
  //   if (window.SITE_CONFIG['sitURL'] === 'https://www.plantmate.com/api') {
  //     location.href = 'https://de-pc.jxt.zj.gov.cn/web-company-pc-v1/index.html#/view/card?type=%E7%9F%B3%E6%B2%B9%E5%8C%96%E5%B7%A5'
  //   } else {
  //     location.href = 'https://detest.jxt.zj.gov.cn/web-company-pc-v1/index.html#/view/card?type=%E7%9F%B3%E6%B2%B9%E5%8C%96%E5%B7%A5'
  //   }
  // }
  // component: () => import('@/views/modules/defaultIndex/index'),
  // name: 'jump',
  // redirect: {
  //   name: 'jump'
  // },
  // meta: {
  //   title: '主入口布局'
  // },
  // children: [
  //   {
  //     path: '/jump',
  //     component: () => import('@/views/modules/defaultIndex/index'),
  //     name: 'jump',
  //     meta: {
  //       title: '首页',
  //       isTab: true
  //     }
  //   }
  // ]
}

export function addDynamicRoute (routeParams, router) {
  // 组装路由名称, 并判断是否已添加, 如是: 则直接跳转
  var routeName = routeParams.routeName
  var dynamicRoute = window.SITE_CONFIG['dynamicRoutes'].filter(
    (item) => item.name === routeName
  )[0]
  if (dynamicRoute) {
    return router.push({
      name: routeName,
      params: routeParams.params
    })
  }
  // 否则: 添加并全局变量保存, 再跳转
  dynamicRoute = {
    path: routeName,
    component: () => import(`@/views/modules/${routeParams.path}`),
    name: routeName,
    meta: {
      ...window.SITE_CONFIG['contentTabDefault'],
      menuId: routeParams.menuId,
      title: `${routeParams.title}`
    }
  }
  router.addRoutes([
    {
      ...moduleRoutes,
      name: `main-dynamic__${dynamicRoute.name}`,
      children: [dynamicRoute]
    }
  ])
  window.SITE_CONFIG['dynamicRoutes'].push(dynamicRoute)
  router.push({
    name: dynamicRoute.name,
    params: routeParams.params
  })
}

const router = new Router({
  mode: 'hash',
  scrollBehavior: () => ({
    y: 0
  }),
  routes: pageRoutes.concat(moduleRoutes)
})

router.beforeEach((to, from, next) => {
  return next()
  // if (
  //   Cookies.get('access_token') &&
  //   Cookies.get('access_token') !== 'undefind' &&
  //   Cookies.get('access_token') !== 'null'
  // ) {
  // } else {
  //   if (to.name === 'login' || (to.meta && to.meta.noNeedToken === true)) {
  //     return next();
  //   }
  //   return next({
  //     name: 'login',
  //   });
  // }
  // // 添加动态(菜单)路由
  // // 已添加或者当前路由为页面路由, 可直接访问
  // if (
  //   window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] ||
  //   fnCurrentRouteIsPageRoute(to, pageRoutes)
  // ) {
  //   return next();
  // }
  // // 获取字典列表, 添加并全局变量保存
  // request
  //   .get('help/user/sys/dict/selectAllDict')
  //   .then((res, total, code, data) => {
  //     if (code !== 0) {
  //       return;
  //     }
  //     window.SITE_CONFIG['dictList'] = res;
  //   })
  //   .catch(() => {});
  // // 获取菜单列表, 添加并全局变量保存
  // http
  //   .get(`/help/user/sys/menu/bar/${window.SITE_CONFIG.appCode}`)
  //   .then(({ data: res }) => {
  //     if (res.code !== 0) {
  //       Vue.prototype.$message.error(res.message);
  //       return next({
  //         name: 'login',
  //       });
  //     }
  //     window.SITE_CONFIG['menuList'] = res.data;
  //     var topMenu = [];
  //     for (let i = 0; i < res.data.length; i++) {
  //       topMenu.push({
  //         id: res.data[i].menuId,
  //         name: res.data[i].menuName,
  //       });
  //     }
  //     window.SITE_CONFIG['topmenuList'] = topMenu;
  //     fnAddDynamicMenuRoutes(window.SITE_CONFIG['menuList']);
  //     next({
  //       ...to,
  //       replace: true
  //     });
  //   })
  //   .catch((err) => {
  //     Vue.prototype.$message.error(err.response.data.message);
  //     next({
  //       name: 'login',
  //     });
  //   });
})

/**
 * 判断当前路由是否为页面路由
 * @param {*} route 当前路由
 * @param {*} pageRoutes 页面路由
 */
function fnCurrentRouteIsPageRoute (route, pageRoutes = []) {
  var temp = []
  for (var i = 0; i < pageRoutes.length; i++) {
    if (route.path === pageRoutes[i].path) {
      return true
    }
    if (pageRoutes[i].children && pageRoutes[i].children.length >= 1) {
      temp = temp.concat(pageRoutes[i].children)
    }
  }
  return temp.length >= 1 ? fnCurrentRouteIsPageRoute(route, temp) : false
}

/**
 * 添加动态(菜单)路由
 * @param {*} menuList 菜单列表
 * @param {*} routes 递归创建的动态(菜单)路由
 */
function fnAddDynamicMenuRoutes (menuList = [], routes = []) {
  var temp = []
  for (var i = 0; i < menuList.length; i++) {
    if (menuList[i].children && menuList[i].children.length >= 1) {
      temp = temp.concat(menuList[i].children)
      continue
    }
    // 组装路由
    var route = {
      path: '',
      component: null,
      name: '',
      meta: {
        ...window.SITE_CONFIG['contentTabDefault'],
        menuId: menuList[i].menuId,
        title: menuList[i].menuName
      }
    }
    // eslint-disable-next-line
    let URL = (menuList[i].menuUrl || '').replace(/{{([^}}]+)?}}/g, (s1, s2) =>
      eval(s2)
    ) // URL支持{{ window.xxx }}占位符变量
    if (isURL(URL)) {
      route['path'] = route['name'] = `i-${menuList[i].menuId}`
      route['meta']['iframeURL'] = URL
    } else {
      URL = URL.replace(/^\//, '').replace(/_/g, '-')
      route['path'] = route['name'] = URL.replace(/\//g, '-')
      route['component'] = () => import(`@/views/modules/${URL}`)
    }
    routes.push(route)
  }
  if (temp.length >= 1) {
    return fnAddDynamicMenuRoutes(temp, routes)
  }
  // 添加路由
  router.addRoutes([
    {
      ...moduleRoutes,
      name: 'main-dynamic-menu',
      children: routes
    },
    {
      path: '*',
      redirect: {
        name: '404'
      }
    }
  ])
  window.SITE_CONFIG['dynamicMenuRoutes'] = routes
  window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] = true
}

export default router
